package com.guodu.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.guodu.dao.UnitsDao;
import com.guodu.dao.ViewMaterialRukuDao;
import com.guodu.entity.Units;
import com.guodu.entity.ViewMaterialRuku;
import com.guodu.service.ViewMaterialRukuService;
import com.guodu.vo.ViewMaterialRukuVo;
import com.guodu.vo.ViewVo;

@Service("viewMaterialRukuService")
public class ViewMaterialRukuServiceImpl implements ViewMaterialRukuService{

	@Resource
	private ViewMaterialRukuDao viewMaterialRukuDao;
	
	@Resource
	private UnitsDao unitsDao;
	
	@Override
	public Map<String, ViewMaterialRukuVo> findByVo(ViewVo vvo) {
		// TODO Auto-generated method stub
		List<Units> funitses = unitsDao.findAllFunits();
		List<ViewMaterialRuku> views = viewMaterialRukuDao.findByVo(vvo);
		Map<String, ViewMaterialRukuVo> map = new HashMap<String, ViewMaterialRukuVo>();
		map.put("all", new ViewMaterialRukuVo(new ViewMaterialRuku()));
		for(Units units : funitses){
			map.put(units.getUnitsId().toString()+"all", new ViewMaterialRukuVo(new ViewMaterialRuku()));
		}
		for(ViewMaterialRuku view:views){
			this.sum(map.get("all"), view);
			this.sum(map.get(view.getFunitsId().toString()+"all"), view);
			map.put(view.getUnitsId().toString(), new ViewMaterialRukuVo(view));
		}
		return map;
	}
	
	private ViewMaterialRukuVo sum(ViewMaterialRukuVo view1,ViewMaterialRuku view2){
		view1.setMonthMoney((view1.getMonthMoney()!=null?view1.getMonthMoney():new BigDecimal(0)).add((view2.getMonthMoney()!=null?view2.getMonthMoney():new BigDecimal(0))));
		view1.setYearMoney((view1.getYearMoney()!=null?view1.getYearMoney():new BigDecimal(0)).add((view2.getYearMoney()!=null?view2.getYearMoney():new BigDecimal(0))));
		return view1;
	}

}
